<template>
    <basic-container>
      <avue-crud
        ref="crud"
        :option="tableOption"
        :data="list"
        v-model="form"
        :table-loading="listLoading"
        :permission="permissionList"
        :before-open="beforeOpen"
        @on-load="getList"
        @search-change="searchChange"
        @search-reset="searchReset"
        @refresh-change="refreshChange"
        @selection-change="selectionChange"
        @current-change="currentChange"
        @size-change="sizeChange"
        @row-del="rowDel"
        @row-update="rowUpdate"
        @row-save="rowSave"
      >

      </avue-crud>

    </basic-container>

</template>

<script>
  import { addObj, delObj, getObj, fetchList, putObj } from '@/api/admin/esindex'
  import { tableOption } from '@/const/crud/admin/esindex'
  import { mapGetters } from 'vuex'

  export default {
    name: 'ESIndex',
    data() {
      return {
        searchForm: {},
        tableOption: tableOption,
        defaultProps: {
          label: 'name',
          value: 'id'
        },
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        selectionList: [],
        list: [],
        listLoading: true,
        form: {},
        dialogVisible: false,
        indexDetail: ''
      }
    },

    computed: {
      ...mapGetters(['elements', 'permissions']),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permissions['sys_esindex_add'], false),
          delBtn: this.vaildData(this.permissions['sys_esindex_del'], false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      getList(page, params) {
        this.listLoading = true
        fetchList(Object.assign({
          current: page.currentPage,
          size: page.pageSize
        }, params, this.searchForm)).then(response => {
          this.list = response.data.data.records
          this.page.total = response.data.data.total
          this.listLoading = false
        }).catch(() => {
          this.listLoading = false
        })
      },
      searchChange(form, done) {
        this.searchForm = form;
        this.page.currentPage = 1;
        this.getList(this.page, form)
        done();
      },
      searchReset() {
        this.searchForm = {};
        this.getList(this.page)
      },

      refreshChange() {
        this.getList(this.page,this.searchForm)
      },

      rowSave(row, done, loading) {
        addObj(this.form).then(() => {
          this.getList(this.page)
          done()
          this.$notify.success('创建成功')
        }).catch(() => {
          loading()
        })
      },
      rowUpdate(row, index, done, loading) {
        putObj(this.form).then(() => {
          this.getList(this.page)
          done()
          this.$notify.success('修改成功')
        }).catch(() => {
          loading()
        })
      },
      rowDel(row) {
        this.$confirm('是否确认删除名称为"' + row.index + '"' + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delObj(row.index)
        }).then(() => {
          this.getList(this.page)
          this.$notify.success('删除成功')
        })
      },

      selectionChange(list) {
        this.selectionList = list;
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },

      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return delObj(this.ids);
          })
          .then(() => {
            this.getList(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      }

    }
  }
</script>

<style>
</style>
